/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Gui;

import Connection.Connect;
import Connection.DataAccess;
import Controller.ChiTietHoaDonController;
import Controller.HoaDonController;
import Model.ChiTietHoaDon;
import Model.DienThoai;
import Model.HoaDon;
import Model.NhanVien;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
//import net.sf.jasperreports.view.*;
//import net.sf.jasperreports.engine.*;

/**
 *
 * @author My PC
 */
public class JFrameHoaDon extends javax.swing.JFrame {

    NhanVien nv = new NhanVien();
    DienThoai dt = new DienThoai();
    HoaDon hd = new HoaDon();
    ChiTietHoaDon cthd = new ChiTietHoaDon();
    public JFrameHoaDon() {
        initComponents();
        LoadTable();
        LoadTableCTHD();
    }

    String maDT;
    String maKH;
    
   public void GetInfo() {
        hd.setMaHoadon(this.txtMaHD.getText());
        hd.setMaKhachhang(this.txtMaKH.getText());   
        hd.setMaNhanvien(this.txtMaNV.getText());
        hd.setNgaylap(convertJavaDateToSqlDate(this.jDNgaylap.getDate()));
        hd.setGhichu(this.txtGhiChu.getText());
        
    }
   
    public void GetInfoCTHD() {
        cthd.setMaHoadon(this.txtMaHD.getText());
        cthd.setMaSanpham(this.txtMaDT.getText());   
        //cthd.setTenSanpham(this.txtMaNV.getText());
        cthd.setSoluong(Integer.parseInt(this.txtSoLuong.getText()));
        
    }

    public java.sql.Date convertJavaDateToSqlDate(java.util.Date date) {
        return new java.sql.Date(date.getTime());
    }

    public java.util.Date convertJavaSqlToDate(java.sql.Date date) {
        return new java.util.Date(date.getTime());
    }
   
    
    public void LoadTable() {

        String sql = " SELECT MA_HOADON, MA_NHANVIEN, MA_KHACHHANG,"
         +" TONGTIEN, NGAYLAP, GHICHU FROM "
         + "tblHoaDon ";
        DataAccess da = new DataAccess();
        da.ShowInfor(sql,jTableHoaDon);
    }
    public void LoadTableCTHD(){
        String sql = "SELECT cthd.MA_HOADON, cthd.MA_SANPHAM, sp.TEN_SANPHAM, "
                + "cthd.SOLUONG, cthd.THANHTIEN FROM tblChiTietHoaDon cthd "
                + "inner join tblSanPham sp on sp.MA_SANPHAM = cthd.MA_SANPHAM ";
        DataAccess da = new DataAccess();
        da.ShowInfor(sql,jTableCTHD);
    }
    
     private void XoaText() {
        this.txtMaHD.setText("");
        this.txtMaKH.setText("");
        this.txtMaNV.setText("");
        this.txtMaDT.setText("");
        this.txtSoLuong.setText("");
        this.txtTongtien.setText("");
        this.txtGhiChu.setText("");
        this.jDNgaylap.setDate(new Date());
    }
    
 
     private void Setcontrol(Boolean edit) {
        this.txtMaHD.setEnabled(false);
        this.txtMaNV.setEnabled(edit);
        this.txtMaKH.setEnabled(edit);
        this.txtSoLuong.setEnabled(edit);
        this.txtTongtien.setEnabled(edit);
        this.txtGhiChu.setEnabled(edit);
        
    }
   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtMaHD = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtGhiChu = new javax.swing.JTextField();
        txtMaKH = new javax.swing.JTextField();
        txtMaNV = new javax.swing.JTextField();
        jDNgaylap = new com.toedter.calendar.JDateChooser();
        jLabel6 = new javax.swing.JLabel();
        txtSoLuong = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtTongtien = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtblDT = new javax.swing.JTable();
        btnThem = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        btnChiTiet = new javax.swing.JButton();
        btnCapnhat = new javax.swing.JButton();
        btnHuy = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();
        btnTimKiem = new javax.swing.JButton();
        txtMaDT = new javax.swing.JTextField();
        btnSearchDT = new javax.swing.JButton();
        txtSearchDT = new javax.swing.JTextField();
        txtSearchKH = new javax.swing.JTextField();
        btnSearchKH = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jtblKH = new javax.swing.JTable();
        btnThemDT = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTableCTHD = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableHoaDon = new javax.swing.JTable();
        btnSua = new javax.swing.JButton();
        btnThemDT1 = new javax.swing.JButton();

        setTitle("Thông Tin Hóa Đơn");

        jPanel2.setBackground(new java.awt.Color(0, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông Tin Hóa Đơn", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 3, 18), new java.awt.Color(0, 0, 0))); // NOI18N

        jLabel1.setText("Mã Hóa Đơn");

        jLabel2.setText("Mã Nhân Viên");

        jLabel3.setText("Mã Khách Hàng");

        jLabel4.setText("Ngày lập");

        txtMaHD.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N

        jLabel7.setText("Ghi chú");

        txtGhiChu.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N

        txtMaKH.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N

        jLabel6.setText("Mã Sản phẩm");

        txtSoLuong.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N

        jLabel8.setText("Số lượng");

        jLabel9.setText("Tổng tiền");

        txtTongtien.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N

        jtblDT.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Mã hóa đơn", "Mã sản phẩm", "Số lượng", "Đơn giá", "Ghi chú"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.String.class, java.lang.Object.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jtblDT.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtblDTMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jtblDT);

        btnThem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/add 16x16.png"))); // NOI18N
        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        btnXoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/delete (3).png"))); // NOI18N
        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        btnChiTiet.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Datbase.png"))); // NOI18N
        btnChiTiet.setText("Chi Tiết");
        btnChiTiet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChiTietActionPerformed(evt);
            }
        });

        btnCapnhat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Reset.png"))); // NOI18N
        btnCapnhat.setText("Cập nhật");
        btnCapnhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCapnhatActionPerformed(evt);
            }
        });

        btnHuy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/disapprove.png"))); // NOI18N
        btnHuy.setText("Hủy");
        btnHuy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHuyActionPerformed(evt);
            }
        });

        btnThoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Exit.png"))); // NOI18N
        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        btnTimKiem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Search (3).png"))); // NOI18N
        btnTimKiem.setText("Tìm kiếm_Mã HĐ");
        btnTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimKiemActionPerformed(evt);
            }
        });

        txtMaDT.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N

        btnSearchDT.setText("Sear_Tên Điện Thoại");
        btnSearchDT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchDTActionPerformed(evt);
            }
        });

        btnSearchKH.setText("Search_Tên Khách Hàng");
        btnSearchKH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchKHActionPerformed(evt);
            }
        });

        jtblKH.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Ma_KH", "Tên_KH", "Giới tính", "Địa chỉ"
            }
        ));
        jtblKH.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtblKHMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(jtblKH);

        btnThemDT.setText("Thêm ĐT");
        btnThemDT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemDTActionPerformed(evt);
            }
        });

        jTableCTHD.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Mã Hóa Đơn", "Mã Sản Phẩm", "Tên Sản Phẩm", "Thành Tiền"
            }
        ));
        jTableCTHD.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableCTHDMouseClicked(evt);
            }
        });
        jScrollPane4.setViewportView(jTableCTHD);

        jLabel5.setFont(new java.awt.Font("Times New Roman", 3, 18)); // NOI18N
        jLabel5.setText("Chi Tiết Hóa Đơn");

        jTableHoaDon.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Mã HD", "Mã NV", "Mã KH", "Ngày lập", "Tổng tiền", "Ghi chú"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTableHoaDon.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableHoaDonMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableHoaDon);

        btnSua.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/edit (3).png"))); // NOI18N
        btnSua.setText("Sửa");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        btnThemDT1.setText("Sửa");
        btnThemDT1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemDT1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(27, 27, 27)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtMaHD, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtMaNV, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 442, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel7)
                                    .addComponent(btnThem, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel9)
                                    .addComponent(btnHuy, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(btnSua)
                                        .addGap(18, 18, 18)
                                        .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnChiTiet))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(btnTimKiem)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(btnThoat)
                                        .addGap(18, 18, 18)
                                        .addComponent(btnCapnhat))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGap(13, 13, 13)
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(txtMaKH, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(txtMaDT, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGap(11, 11, 11)
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jDNgaylap, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(txtSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(txtGhiChu, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(txtTongtien, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                        .addGap(25, 25, 25)))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(42, 42, 42)
                                .addComponent(btnSearchKH)
                                .addGap(18, 18, 18)
                                .addComponent(txtSearchKH, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(btnSearchDT)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtSearchDT, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(26, 26, 26)
                                .addComponent(btnThemDT)
                                .addGap(30, 30, 30)
                                .addComponent(btnThemDT1, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 539, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel10)
                        .addGap(201, 201, 201)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(338, 338, 338))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 541, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 541, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnSearchDT)
                            .addComponent(txtSearchDT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnThemDT)
                            .addComponent(btnThemDT1)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtMaHD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtMaNV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtMaKH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(txtMaDT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(14, 14, 14)
                                .addComponent(jLabel8))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(20, 20, 20)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnSearchKH)
                            .addComponent(txtSearchKH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(31, 31, 31)
                        .addComponent(jLabel5))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(9, 9, 9)
                                .addComponent(jLabel4)
                                .addGap(24, 24, 24)
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtTongtien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(15, 15, 15)
                                .addComponent(jDNgaylap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtGhiChu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnThem)
                            .addComponent(btnSua)
                            .addComponent(btnXoa)
                            .addComponent(btnChiTiet))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnCapnhat, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(btnHuy)
                                .addComponent(btnTimKiem)
                                .addComponent(btnThoat)))))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(23, 23, 23)
                .addComponent(jLabel10))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 1060, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtblDTMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtblDTMouseClicked
        // TODO add your handling code here:
        int dong=jtblDT.getSelectedRow();
        txtMaDT.setText((String)this.jtblDT.getValueAt(dong,0));
    }//GEN-LAST:event_jtblDTMouseClicked

    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        if (this.txtMaHD.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Bạn phải nhập mã điện thoại ");
        } else {
            GetInfo();
            if (HoaDonController.ThemHD(hd)) {
                JOptionPane.showMessageDialog(this, "Thêm hóa đơn thành công");
                // XoaText();
            } else {
                JOptionPane.showMessageDialog(this, "Thêm hóa đơn thất bại");
            }
            LoadTable();

        }
    }//GEN-LAST:event_btnThemActionPerformed

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        GetInfo();
        if(HoaDonController.XoaHD(hd)){
            JOptionPane.showMessageDialog(this, "Chi tiết hóa đơn sẽ bị xóa !!!");
        }
        else{
            JOptionPane.showMessageDialog(this,"Không xoá được hóa đơn vì tồn tại hoá đơn chứa ĐT này");
        }

        LoadTable();
    }//GEN-LAST:event_btnXoaActionPerformed

    private void btnChiTietActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChiTietActionPerformed
        String ma_HD = this.txtMaHD.getText();
        if(ma_HD.trim().equals("")){
            JOptionPane.showMessageDialog(this, "Bạn chua chọn đối tượng");
        }
        else{
            String sql = "SELECT cthd.MA_HOADON, cthd.MA_SANPHAM, sp.TEN_SANPHAM,"
            + " cthd.SOLUONG, cthd.THANHTIEN FROM tblChiTietHoaDon cthd "
            + "inner join tblSanPham sp on sp.MA_SANPHAM = cthd.MA_SANPHAM "
            + " where MA_HOADON = '"+ma_HD+"'";
            DataAccess da = new DataAccess();
            da.ShowInfor(sql, jTableCTHD);
        }
    }//GEN-LAST:event_btnChiTietActionPerformed

    private void btnCapnhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCapnhatActionPerformed
        // TODO add your handling code here:
        LoadTable();
        LoadTableCTHD();
    }//GEN-LAST:event_btnCapnhatActionPerformed

    private void btnHuyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHuyActionPerformed
        // TODO add your handling code here:
        XoaText();
    }//GEN-LAST:event_btnHuyActionPerformed

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_btnThoatActionPerformed

    private void btnTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimKiemActionPerformed
        // TODO add your handling code here:
        String ma_HD = this.txtMaHD.getText();
        if(ma_HD.trim().equals("")){
            JOptionPane.showMessageDialog(null,"Bạn nhập mã hóa đơn !");
        }
        else{
            String sql = "SELECT MA_HOADON, MA_NHANVIEN, MA_KHACHHANG,"
            +" TONGTIEN, NGAYLAP, GHICHU FROM tblHoaDon "
            + " where MA_HOADON like N'%"+ma_HD+"%'";
            DataAccess da = new DataAccess();
            da.ShowInfor(sql, jTableHoaDon);
        }
    }//GEN-LAST:event_btnTimKiemActionPerformed

    private void btnSearchDTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchDTActionPerformed
        // TODO add your handling code here:
        if(this.txtSearchDT.getText().trim().equals(""))
        {
            JOptionPane.showMessageDialog(null, "Ban phai nhap ten dien thoai !");
        }
        else{
            String sql = "SELECT  S.MA_SANPHAM, S.TEN_SANPHAM, S.MOTA, S.DONGIA,S.SOLUONG,"
            + " S.BAOHANH,S.MA_NHACUNGCAP "
            + "FROM tblSanPham S WHERE S.TEN_SANPHAM like N'%"+txtSearchDT.getText()+"%'";
            DataAccess da = new DataAccess();
            da.ShowInfor(sql,jtblDT);
        }
    }//GEN-LAST:event_btnSearchDTActionPerformed

    private void btnSearchKHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchKHActionPerformed
        // TODO add your handling code here:
        if(this.txtSearchDT.getText().trim().equals(""))
        {
            JOptionPane.showMessageDialog(null, "Ban phai nhap ten khach hang !");
        }
        else{
            String sql = "select MA_KHACHHANG,TEN_KHACHHANG,NGAYSINH,DIACHI,"
            + "LOAI_KHACHHANG,GIOITINH,SO_CMND,DIENTHOAI,EMAIL,"
            + "NGAY_MUA from tblKhachHang WHERE TEN_KHACHHANG"
            + " like N'%"+txtSearchKH.getText()+"%'";
            DataAccess da = new DataAccess();
            da.ShowInfor(sql,jtblKH);
        }
    }//GEN-LAST:event_btnSearchKHActionPerformed

    private void jtblKHMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtblKHMouseClicked
        // TODO add your handling code here:
        int dong=jtblKH.getSelectedRow();
        txtMaKH.setText((String)this.jtblKH.getValueAt(dong,0));
    }//GEN-LAST:event_jtblKHMouseClicked

    private void btnThemDTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemDTActionPerformed
        // TODO add your handling code here
        GetInfoCTHD();
        if(ChiTietHoaDonController.ThemCTHD(cthd)==false)
        {

            if(ChiTietHoaDonController.SuaCTHD(cthd));
            else {
                JOptionPane.showMessageDialog(this,"Thêm hoá đơn không thành công ");
            }
        }
        HoaDonController.UpdTongTien(hd);

        LoadTableCTHD();
        LoadTable();
    }//GEN-LAST:event_btnThemDTActionPerformed

    private void jTableCTHDMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableCTHDMouseClicked
        // TODO add your handling code here:
        int dong=jTableCTHD.getSelectedRow();
        txtMaHD.setText((String)this.jTableCTHD.getValueAt(dong, 0));
        txtMaDT.setText((String)jTableCTHD.getValueAt(dong, 1));
        txtSoLuong.setText((String)this.jTableCTHD.getValueAt(dong, 3));
    }//GEN-LAST:event_jTableCTHDMouseClicked

    private void jTableHoaDonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableHoaDonMouseClicked
        // TODO add your handling code here:
        int dong=jTableHoaDon.getSelectedRow();
        txtMaHD.setText((String)this.jTableHoaDon.getValueAt(dong, 0));
        txtMaNV.setText((String)jTableHoaDon.getValueAt(dong, 1));
        txtMaKH.setText((String)jTableHoaDon.getValueAt(dong, 2));
        txtTongtien.setText((String)jTableHoaDon.getValueAt(dong, 3));
        txtGhiChu.setText((String)jTableHoaDon.getValueAt(dong, 5));
        String pattern = "yyyy-MM-dd";
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = format.parse(this.jTableHoaDon.getValueAt(dong, 4).toString());
        } catch (ParseException ex) {
            Logger.getLogger(JFrameHoaDon.class.getName()).log(Level.SEVERE, null, ex);
        }
        jDNgaylap.setDate(date);
    }//GEN-LAST:event_jTableHoaDonMouseClicked

    private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
        String ma_HD = this.txtMaHD.getText();
        if(ma_HD.trim().equals("")){
            JOptionPane.showMessageDialog(this, "Bạn chưa chọn đối tượng !");
        }
        else{
            GetInfo();
            if(HoaDonController.SuaHD(hd))
            {
                JOptionPane.showMessageDialog(this, "Sửa hóa đơn thành công!");
            }
            else{
                JOptionPane.showMessageDialog(this, "Sửa Hóa đơn không thành công!");
            }
        }
    }//GEN-LAST:event_btnSuaActionPerformed

    private void btnThemDT1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemDT1ActionPerformed
        // TODO add your handling code here:
        String ma_HD = this.txtMaHD.getText();
        if(ma_HD.trim().equals("")){
            JOptionPane.showMessageDialog(this,"Bạn chưa chọn đối tượng cần sửa");
        }
        else{
            if(ChiTietHoaDonController.SuaCTHD(cthd))
            {
                JOptionPane.showMessageDialog(this, "Sửa Chi Tiết Hóa Đơn thành công!");
            }
            else {
                JOptionPane.showMessageDialog(this, "Sửa Chi Tiết Hóa Đơn không thành công!");
            }
        }
    }//GEN-LAST:event_btnThemDT1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFrameHoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFrameHoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFrameHoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFrameHoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFrameHoaDon().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCapnhat;
    private javax.swing.JButton btnChiTiet;
    private javax.swing.JButton btnHuy;
    private javax.swing.JButton btnSearchDT;
    private javax.swing.JButton btnSearchKH;
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnThemDT;
    private javax.swing.JButton btnThemDT1;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnTimKiem;
    private javax.swing.JButton btnXoa;
    private com.toedter.calendar.JDateChooser jDNgaylap;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable jTableCTHD;
    private javax.swing.JTable jTableHoaDon;
    private javax.swing.JTable jtblDT;
    private javax.swing.JTable jtblKH;
    private javax.swing.JTextField txtGhiChu;
    private javax.swing.JTextField txtMaDT;
    private javax.swing.JTextField txtMaHD;
    private javax.swing.JTextField txtMaKH;
    private javax.swing.JTextField txtMaNV;
    private javax.swing.JTextField txtSearchDT;
    private javax.swing.JTextField txtSearchKH;
    private javax.swing.JTextField txtSoLuong;
    private javax.swing.JTextField txtTongtien;
    // End of variables declaration//GEN-END:variables
}
